#pragma once

#include <stdexcept>
#include <fstream>

#include "util/fs.h"

#include "index-item.h"
#include "uri-path.h"


namespace book
{

class BookMenuConvert
{
public:

    class DirCheckError : public std::logic_error
    {
    public:
        DirCheckError(const char *msg) : std::logic_error(msg) {}
        virtual ~DirCheckError() {}
    };

    class ChapterScope
    {
    public:
        ChapterScope(BookMenuConvert &convert, const UriPath &uri, const IndexItem &indexItem)
            : convert_(convert)
            , uri_(uri)
            , indexItem_(indexItem) {
            convert_.notifyEnterChapter(uri, indexItem);
        }

        ~ChapterScope() {
            convert_.notifyLeaveChapter(uri_, indexItem_);
        }

    private:
        BookMenuConvert &convert_;
        const UriPath &uri_;
        const IndexItem &indexItem_;
    };

    BookMenuConvert(const fs::path &destDir);

    ~BookMenuConvert();

    void notifyEnterChapter(const UriPath &chapterUri, const IndexItem &indexItem);

    void notifyLeaveChapter(const UriPath &chapterUri, const IndexItem &indexItem);

    void notifyMeetArticle(const UriPath &itemUri, const IndexItem &indexItem);

private:
    std::ofstream out_;
};

} // namespace book
